package cn.edu.nyist.integration.service.impl;

import cn.edu.nyist.integration.common.Const;
import cn.edu.nyist.integration.common.ServerResponse;
import cn.edu.nyist.integration.entity.Notice;
import cn.edu.nyist.integration.mapper.NoticeMapper;
import cn.edu.nyist.integration.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;


    /**
     * 管理员发布公告
     */
    @Override
    public ServerResponse issueNotice(Notice notice) {
        int row = noticeMapper.insert(notice);
        if (row == 0) {
            return ServerResponse.createByErrorMessage("发布失败");
        }
        return ServerResponse.createBySuccessMessage("发布成功");
    }


    @Override
    public List<Notice> queryAll() {

        List<Notice> notices = noticeMapper.selectAll();
        return notices;

    }


    @Override
    public ServerResponse updateNotice(Notice notice) {
        if (notice == null) {
            return ServerResponse.createByErrorMessage(Const.ILLEGAL_ARGUMENT);
        }
        int row = noticeMapper.updateByPrimaryKeySelective(notice);
        if (row == 0) {
            return ServerResponse.createByErrorMessage("修改失败");
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse deleteNotice(Long id) {
        int count = noticeMapper.deleteByPrimaryKey(id);
        if (count == 0) {
            return ServerResponse.createByErrorMessage("删除失败");
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public Notice selectOne(Long id) {

        Notice notice = noticeMapper.selectByPrimaryKey(id);
        return notice;
    }

    /**
     * 找出最新的公告
     *
     * @return
     */
    @Override
    public ServerResponse getFirstNotice() {
        Notice notice = noticeMapper.selectFirstNotice();
        System.out.println(notice);
        return ServerResponse.createBySuccessData(notice);
    }

    @Override
    public ServerResponse findNotice(Long noticeId) {
        if (noticeId == null){
            return ServerResponse.createByErrorMessage(Const.ILLEGAL_ARGUMENT);
        }
        Notice notice = noticeMapper.selectByPrimaryKey(noticeId);

        return ServerResponse.createBySuccessData(notice);
    }

}
